<template>
  <el-dialog width="600px" title="添加PDA" :visible.sync="popupVisible" :before-close="close">
    <el-form align="left" ref="formInline" :rules="ruleForm" :model="formInline" label-width="120px">
      <el-row>
        <el-col :span="22">
          <el-form-item label="上传PDA文件" required>
            <el-upload
              ref="upload"
              action="#"
              :limit="1"
              :on-exceed="handleExceed"
              :on-remove="handleRemove"
              :file-list="fileList"
              :auto-upload="false">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <div slot="tip" class="el-upload__tip">只能上传apk文件</div>
            </el-upload>
          </el-form-item>
          <el-form-item label="是否强制更新">
            <el-switch
              style="display: block"
              v-model="formInline.forceupdate"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="是"
              inactive-text="否">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="版本code" prop="versioncode">
            <el-input maxlength="20" show-word-limit v-model.number="formInline.versioncode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="版本名称" prop="versionname">
            <el-input maxlength="20" show-word-limit v-model="formInline.versionname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" align="center">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import {formValidate} from '@/assets/mixins/rule/pda-validate'
export default {
  name:'handle-pda-popup',
  props:['popupVisible'],
  mixins:[formValidate],
  data(){
    return {
      formInline:{
        forceupdate:true, // 是否强制更新/1:是，0:否
        versioncode:'', // 版本code,正整数区分版本先后
        versionname:'' // 版本名称,展示给用户的版本名称
      },

      fileList:[] // 上传的文件
    }
  },
  mounted(){

  },
  components:{

  },
  methods:{
    close(){
      this.$emit('close')
    },
    handleRemove(file, fileList){

    },
    handleExceed(){
      this.callbackMessage('只能上传1个APK文件!')
    },
    handleBeforeUpload(file){
      const isAPK = file.raw.type.includes('android.package')
      if (!isAPK) return this.callbackMessage('上传文件只能是 APK 格式!')
      return isAPK
    },
    submit(){
      if(!this.$refs.upload.uploadFiles.length) return this.callbackMessage('请选择上传的文件')
      this.$refs['formInline'].validate((valid)=>{
        if(valid){
          let file = this.$refs.upload.uploadFiles[0]
          let isAPK = this.handleBeforeUpload(file)
          if(isAPK){
            const formData = new FormData()
            formData.append('file', file.raw)
            let params = this.cloneObject(this.formInline)
            params.forceupdate = Number(params.forceupdate)
            let options = {
              headers:{ 'Content-Type':'multipart/form-data' },
              method:'post',
              url:`/pdaversion/upload`,
              params,
              data:formData
            }
            this.$axios(options).then(() => {
              this.callbackMessage('上传成功！')
              this.$emit('callback')
              this.$emit('close')
            })
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-switch{
  line-height: 40px;
  & ::v-deep .el-switch__label{height: auto;}
}
</style>